package com.cremamobile.filemanager.gridview;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.cremamobile.filemanager.R;
import com.cremamobile.filemanager.file.DirFileEntry;
import com.cremamobile.filemanager.file.FileListEntry;
import com.cremamobile.filemanager.file.FileUtils;
import com.cremamobile.filemanager.file.IconMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter {
	public static final int NOT_SET = -1;
	
	public static final int VIEW_MODE_GRID = 0;
	public static final int VIEW_MODE_LIST = 1;
	
	List<FileListEntry>	mFiles;
	Context	mContext;
	boolean mSelectMode;
	int mViewMode;
	
	boolean selectAll;
	int beforeSelectedIndex = NOT_SET;
	
	IconMap mIconMap;
	
	public FileListAdapter(Context context) {
		mContext = context;
		mIconMap = IconMap.createIconMap(context);
	}
	
	public FileListAdapter(Context context, List<FileListEntry> files) {
		mContext = context;
		mFiles = files;
		mIconMap = IconMap.createIconMap(context);
	}
	
	public void setViewMode(int viewMode) {
		mViewMode = viewMode;
	}
	
	public int getViewMode() {
		return mViewMode;
	}
	
	public void setSelectMode(boolean bMode) {
		mSelectMode = bMode;
		
		if (bMode == false) {
			clearSelect();
		}
		notifyDataSetChanged();
	}
	
	public boolean isSelectMode() {
		return mSelectMode;
	}
	
	public int beforeSelectedIndex() {
		return beforeSelectedIndex;
	}
	
	public void toggleSelect(int index) {
		FileListEntry file = mFiles.get(index);
		file.setSelected( ! file.isSelected() );

		beforeSelectedIndex = index;
		notifyDataSetChanged();

	}
	
	public void toggleSelectAll() {
		selectAll = ! selectAll;
		for (FileListEntry file : mFiles) {
			file.setSelected(selectAll);
		}
		
		notifyDataSetChanged();
	}
	
	public void clearSelect() {
		selectAll = false;
		beforeSelectedIndex = NOT_SET;
		for (FileListEntry file : mFiles) {
			file.setSelected(false);
		}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mFiles == null) return 0;
		return mFiles.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (mFiles == null) return null;
		return mFiles.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (convertView == null) {
//			grid = new View(mContext);
			if (mViewMode == VIEW_MODE_GRID)
				convertView = inflater.inflate(R.layout.grid_item, parent, false);
			else 
				convertView = inflater.inflate(R.layout.list_item, parent, false);
				
		} 

//		if(position < chacheImgList.size())
//			imageView.setImageBitmap(chacheImgList.get(position));
		
		return updateView(convertView, position);
	}

	private View updateView(View view, int position) {
		FileListEntry file = (FileListEntry)getItem(position);
		
		ImageView fileImageView = null;
		ImageView selectedImageView = null;
		
		if (file.isDir()) {
			FrameLayout layout = (FrameLayout) view.findViewById(R.id.icon_dir);
			layout.setVisibility(View.VISIBLE);
			FrameLayout file_layout = (FrameLayout) view.findViewById(R.id.icon_file);
			file_layout.setVisibility(View.GONE);
			
	        fileImageView = (ImageView) layout.findViewById(R.id.dir_image);
	        selectedImageView = (ImageView) layout.findViewById(R.id.dir_selected_image);
		} else {
			FrameLayout layout = (FrameLayout) view.findViewById(R.id.icon_file);
			layout.setVisibility(View.VISIBLE);
			FrameLayout dir_layout = (FrameLayout) view.findViewById(R.id.icon_dir);
			dir_layout.setVisibility(View.GONE);
			
	        fileImageView = (ImageView) layout.findViewById(R.id.file_image);
	        selectedImageView = (ImageView) layout.findViewById(R.id.file_selected_image);
	        
			// name 
			TextView fileExtView = (TextView) view.findViewById(R.id.file_ext);
			String fileExt = file.getExt();
			if (fileExt != null) {
				IconMap iconMap = IconMap.createIconMap(mContext);
				int icontype = iconMap.getIconType(fileExt);
				if (icontype == IconMap.FILE_TYPE_APK)
					fileExtView.setText("");
				else
					fileExtView.setText(file.getExt().toUpperCase());
			}
			else
				fileExtView.setText("NONE");
		}

		// name 
		TextView fileNameView = (TextView) view.findViewById(R.id.file_name);
        fileNameView.setText(file.getFullName());

        if (mViewMode == VIEW_MODE_LIST) {
    		TextView fileInfoView = (TextView) view.findViewById(R.id.file_info);
    		TextView fileChildInfoView = (TextView) view.findViewById(R.id.file_child_info);
    		
        	if (file.isNavigationEntry()) {
        		fileInfoView.setText("");
        		fileChildInfoView.setText("");
        	} else {
        		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        		fileInfoView.setText(format.format(file.getLastModified()));
        		
        		if (file.isDir()) {
        			DirFileEntry dir = (DirFileEntry) file;
            		fileChildInfoView.setText(mContext.getResources().getString(R.string.items, dir.getChildFileNumber()));
        		} else {
            		fileChildInfoView.setText(FileUtils.formatSize(file.getSize()));
        		}
        	}
    		
	        selectedImageView = (ImageView) view.findViewById(R.id.selected_image);
        }
        
        fileImageView.setImageBitmap(getFileImageBitmap(file));
        if (mSelectMode && !file.isNavigationEntry()) {
        	selectedImageView.setVisibility(View.VISIBLE);
	        if (file.isSelected()) {
	        	//TODO.
	        	selectedImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.orange_check));
	        } else {
	        	selectedImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.non_check));
	        }
        } else {
        	selectedImageView.setVisibility(View.GONE);
        }
		return view;
	}
	
	private Bitmap getFileImageBitmap(FileListEntry file) {
		return file.getIconBitmap(mContext);
	}
	
	public void setList(List<FileListEntry> files) {
		mFiles = null;
		mFiles = files;
		
		notifyDataSetChanged();
	}
	
	public List<FileListEntry> getList() {
		return mFiles;
	}
}
